package com.freshcity.lib.tooltip
{
	import com.freshcity.lib.util.ObjectPool;

	import flash.accessibility.AccessibilityProperties;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	import com.freshcity.lib.util.ObjectPool;

	public class ToolTipsManager extends Sprite
	{
		private var area:DisplayObject;

		private var toolTipLayer:DisplayObjectContainer;

		private var delayTimer:Timer;

		private var toolTipContent:IToolTip;

		private var _this:ToolTipsManager;

		private static var toolClassMap:Object={};

		private static var classId:int=0;

		private static var _instance:ToolTipsManager=null;

		private static var _defaultRenderClass:Class;

		public function ToolTipsManager(arg1:DisplayObjectContainer)
		{
			super();
			this.visible=false;
			this.mouseChildren=false;
			this.mouseEnabled=false;
			this.toolTipLayer=arg1;
			this._this=this;
			arg1.addChild(this);
		}

		public function targetMouseHandler(arg1:MouseEvent):void
		{
			var loc1:*=null;
			var loc2:*=arg1.type;
			switch (loc2)
			{
				case MouseEvent.MOUSE_OUT:
				case MouseEvent.MOUSE_DOWN:
				case MouseEvent.MOUSE_UP:
				{
					_instance.hide();
					break;
				}
				case MouseEvent.MOUSE_MOVE:
				{
					_instance.move(new flash.geom.Point(arg1.stageX, arg1.stageY));
					break;
				}
				case MouseEvent.MOUSE_OVER:
				{
					loc1=arg1.currentTarget as DisplayObject;
					if (_instance.area != loc1)
					{
						_instance.hide();
					}
					if (loc1.alpha == 1)
					{
						_instance.area=arg1.currentTarget as DisplayObject;
						this.startDelayCount();
					}
					break;
				}
			}
			return;
		}

		public function show(arg1:DisplayObject):void
		{
			var loc1:*=null;
			var loc2:*=null;
			var loc3:*=null;
			this.area.addEventListener(MouseEvent.MOUSE_MOVE, this.targetMouseHandler);
			if (arg1.accessibilityProperties)
			{
				this.clearConetnt();
				loc1=toolClassMap[arg1.accessibilityProperties.description];
				if (loc1.areaItem is IToolTipItem)
				{
					loc2=(loc1.areaItem as IToolTipItem).toolTipData;
				}
				else
				{
					loc2=loc1.toolTipData;
				}

				if (loc2 != null)
				{
					this.toolTipContent=ObjectPool.getObject(loc1.toolTipClass as Class);
					(this.toolTipContent as DisplayObject).visible=true;
					this.toolTipContent.data=loc2;
					(loc3=this.toolTipContent as DisplayObjectContainer).mouseChildren=false;
					loc3.mouseEnabled=false;
					addChild(loc3);
				}
			}
			return;
		}

		public function hide():void
		{
			this.clearDelayCount();
			this.clearConetnt();
			if (this.area)
			{
				this.area.removeEventListener(MouseEvent.MOUSE_DOWN, this.targetMouseHandler);
				this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.targetMouseHandler);
				this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.targetMouseHandler);
				this.area=null;
			}
			visible=false;
			return;
		}

		private function clearConetnt():void
		{
			var loc1:*=null;
			if (this.toolTipContent)
			{
				ObjectPool.disposeObject(this.toolTipContent);
				loc1=this.toolTipContent as DisplayObject;
				loc1.visible=false;
				if (loc1.hasOwnProperty("dispose"))
				{
					var loc2:*;
					(loc2=loc1)["dispose"]();
				}
				if (this.contains(loc1))
				{
					this.removeChild(loc1);
				}
				this.toolTipContent=null;
			}
			return;
		}

		public function move(arg1:Point):void
		{
			var loc1:*=this.parent.globalToLocal(arg1);
			this.x=loc1.x + 15;
			if (this.x > this.toolTipLayer.stage.stageWidth - this.width - 6)
			{
				this.x=loc1.x - this.width - 6;
			}
			var loc2:*=1;
			this.y=loc1.y + 6;
			if (this.y + this.height > this.toolTipLayer.stage.stageHeight)
			{
				this.y=this.toolTipLayer.stage.stageHeight - this.height;
			}
			if (!visible)
			{
				visible=true;
			}
			return;
		}

		private function startDelayCount():void
		{
			if (!this.delayTimer)
			{
				this.delayTimer=new Timer(10, 1);
				this.delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.showAfterDelay);
			}
			this.delayTimer.reset();
			this.delayTimer.start();
			this.area.addEventListener(MouseEvent.MOUSE_DOWN, this.targetMouseHandler);
			this.area.addEventListener(MouseEvent.MOUSE_OUT, this.targetMouseHandler);
			this.area.addEventListener(MouseEvent.MOUSE_UP, this.targetMouseHandler);
			return;
		}

		private function clearDelayCount():void
		{
			if (this.delayTimer)
			{
				this.delayTimer.stop();
			}
			return;
		}

		private function showAfterDelay(arg1:TimerEvent):void
		{
			var loc1:*=null;
			if (this.area)
			{
				this.show(this.area);
				loc1=new flash.geom.Point(this.area.mouseX, this.area.mouseY);
				loc1=this.area.localToGlobal(loc1);
				this.move(loc1);
			}
			return;
		}

		public static function get defaultRenderClass():Class
		{
			return _defaultRenderClass;
		}

		public static function init(arg1:DisplayObjectContainer, arg2:Class):void
		{
			if (_instance == null)
			{
				_instance=new ToolTipsManager(arg1);
				_defaultRenderClass=arg2;
			}
		}

		public static function register(arg1:*, arg2:Class=null):void
		{
			var loc1:*=null;
			if (_instance == null)
			{
				throw new Error("ToolTipsManager 未初始化");
			}
			var loc2:*;
			if ((loc2=arg1["accessibilityProperties"]) != null)
			{
				loc1=loc2.description;
			}
			else
			{
				var loc4:*;
				arg1["accessibilityProperties"]=loc4=new AccessibilityProperties();
				loc2=loc4;
				classId++;
				loc1="Item" + classId;
				loc2.description=loc1;
			}
			(arg1 as EventDispatcher).addEventListener(MouseEvent.MOUSE_OVER, _instance.targetMouseHandler);
			if (arg2 == null)
			{
				if (_defaultRenderClass == null)
				{
					throw new Error("ToolTipManger的dfaultRenderClass未定义");
				}
				else
				{
					toolClassMap[loc1]={"toolTipClass": _defaultRenderClass, "areaItem": arg1, "toolTipData": arg1.toolTipData};
				}
			}
			else
			{
				toolClassMap[loc1]={"toolTipClass": arg2, "areaItem": arg1, "toolTipData": arg1.toolTipData};
			}
			return;
		}

		public static function unregister(arg1:EventDispatcher):void
		{
			if (!(_instance == null) && arg1)
			{
				arg1.removeEventListener(MouseEvent.MOUSE_OVER, _instance.targetMouseHandler);
			}
			return;
		}

	}
}
